import { request } from "../../request/request"
import { baseUrl } from "../../utils/util"
Page({
  data: {
    userinfo: {},
    areaArray: [['技术中心'], ['软件工程部'], ['请选择', '移动智能空间软件科', '软件架构科', '软件系统集成科', '软件测试科', 'ESC软件科']],

    // 注册用户需要的条件
    nickName: '',
    gender: '',
    userPosition: '',
    area: '',

    areaCurrentIndex: -1
  },
  onLoad (options) {
    const userinfo = wx.getStorageSync("userinfo");
    this.setData({
      userinfo,
      nickName: userinfo.nickName,
      gender: userinfo.gender
    })
  },

  areaChoosed (event) {
    let { value } = event.detail
    let area = value[2] + 2
    this.setData({
      areaCurrentIndex: value[2],
      area
    })
    console.log(value);
  },

  /**
   * 取消按钮被点击
   * 
   * @param {object} e 点击事件对象
   */
  cancelBtn (e) {
    // 1. 消除数据
    this.remove()
    // 2. 返回上一页
    wx.switchTab({
      url: '/pages/mine/mine'
    });
  },

  /**
   * 确认按钮被点击
   * 
   * @param {objuct} e 点击事件对象
   */
  async checkBtn (e) {
    if (!(this.data.nickName != '' && this.data.gender != '' && this.data.userPosition != '' && this.data.area != '')) {
      wx.showToast({
        title: '还有未填写的条款',
        icon: 'none',
        duration: 2000
      })
    }
    // 没有选择区域，职位不是总监，不能提交
    if (this.data.area == 2 && this.data.userPosition != 3) {
      wx.showToast({
        title: '请选择科室',
        icon: 'none',
        duration: 2000
      })
    }

    // 可以提交注册申请
    let token = wx.getStorageSync('token');
    let userId = wx.getStorageSync('userId');
    let userinfo = wx.getStorageSync('userinfo');
    let nickName = this.data.nickName;
    let gender = this.data.gender;
    let userPosition = this.data.userPosition;
    let area = this.data.area;
    // 住址信息
    let country = userinfo.country
    let province = userinfo.province
    let city = userinfo.city

    const res = await request({
      method: 'post',
      url: `${baseUrl}/register`,
      data: {
        token,
        userId,
        nickName,
        gender,
        userPosition,
        area,
        country,
        province,
        city
      }
    });
    console.log(res);
    let { meta } = res.data
    if (meta.status !== 200) {
      wx.showToast({
        title: '注册失败，请联系管理员',
        icon: 'none',
        duration: 1500
      });
    }
    // 注册成功
    wx.showToast({
      title: '注册用户成功',
      icon: 'none',
      duration: 1500
    });
    this.remove();
    wx.switchTab({
      url: '/pages/mine/mine'
    });
  },

  /**
   * 去除数据项
   */
  remove () {
    this.setData({
      userinfo: {},
      nickName: '',
      gender: '',
      userPosition: '',
      area: '',
      areaCurrentIndex: -1
    })
  },

  /**
   * 监听用户名输入框事件
   */
  nickNameChanged (e) {
    let nickName = e.detail.value
    this.setData({
      nickName
    })
  },

  /**
   * 性别改变触发的事件项
   */
  genderChanged (e) {
    // detail = {value:[选中的radio的value的数组]}
    let gender = e.detail.value === 'male' ? 1 : 2

    this.setData({
      gender
    })
  },

  /**
   * 职位改变触发的事件项
   */
  positionChanged (e) {
    let userPosition = e.detail.value
    this.setData({
      userPosition
    })
  }
});